package com.dlsy.ygj.dao.vindicateData.impl;

import java.sql.SQLException;
import java.util.List;

import net.taobaoge.common.framework.dao.impl.BaseDAOImpl;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.util.CollectionUtils;

import com.dlsy.ygj.dao.vindicateData.IFavouredPackageInfoDAO;
import com.dlsy.ygj.entity.FavouredPackageInfo;

public class FavouredPackageInfoDAO extends BaseDAOImpl<FavouredPackageInfo> implements IFavouredPackageInfoDAO {

	@SuppressWarnings("unchecked")
	public List<FavouredPackageInfo> findFavouredPackageInfoByProperty(
			String value, Integer stroreId) {
		Criteria criteria = getSession()
				.createCriteria(FavouredPackageInfo.class);
		criteria.add(Restrictions.and(Restrictions.or(Restrictions.like(
				"favouredNo", "%" + value + "%"), Restrictions.like(
				"favouredName", "%" + value + "%")), Restrictions.eq(
				"storefrontId", stroreId)));
		List<FavouredPackageInfo> favouredPackageInfos = criteria.list();
		if (favouredPackageInfos != null && favouredPackageInfos.size() > 0) {
			return favouredPackageInfos;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<FavouredPackageInfo> checkExist(final Integer favouredId,
			final String value, final Integer stroreId) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public List<FavouredPackageInfo> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = getSession().createCriteria(
						FavouredPackageInfo.class);
				criteria.add(Restrictions.and(Restrictions.and(Restrictions.or(
						Restrictions.eq("favouredNo", value), Restrictions
								.eq("favouredName", value)), Restrictions
						.not(Restrictions
								.eq("favouredId", favouredId))),
						Restrictions.eq("storefrontId", stroreId)));
				List<FavouredPackageInfo> favouredPackageInfos = criteria.list();
				if (favouredPackageInfos != null && favouredPackageInfos.size() > 0) {
					return favouredPackageInfos;
				}
				return null;
			}
		});
	}

	public List<FavouredPackageInfo> findAll(Integer stroreId) {
		try {
			List<FavouredPackageInfo> favouredPackageInfos = findByProperty(
					FavouredPackageInfo.class, "storefrontId", stroreId);
			if (!CollectionUtils.isEmpty(favouredPackageInfos)) {
				return favouredPackageInfos;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<FavouredPackageInfo> checkExist(final String value,
			final Integer stroreId) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public List<FavouredPackageInfo> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = getSession().createCriteria(
						FavouredPackageInfo.class);
				criteria.add(Restrictions.and(Restrictions.or(Restrictions.eq(
						"favouredNo", value), Restrictions.eq(
						"favouredName", value)), Restrictions.eq(
						"storefrontId", stroreId)));
				List<FavouredPackageInfo> favouredPackageInfos = criteria.list();
				if (favouredPackageInfos != null && favouredPackageInfos.size() > 0) {
					return favouredPackageInfos;
				}
				return null;
			}
		});
	}
}
